﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Core.Domain.Catalog;
using Shop.Core.Domain.Common;
using Shop.Infrastructure.Extensions;
using Shop.Core.Domain.Customers;

namespace Shop.Core.Domain.Orders
{
    public partial class Order
    {
        public virtual OrderProduct AddProduct(Product product, int quantity)
        {
            return this.AddProductCore(product, quantity, OrderProductType.Common);
        }

        public virtual OrderProduct AddGift(Product product, int quantity)
        {
            return this.AddProductCore(product, quantity, OrderProductType.Gift);
        }

        /// <summary>
        /// 设置收货地址
        /// </summary>
        /// <param name="address"></param>
        public virtual void ShipTo(Address address)
        {
            this.ShippingAddress = address.DeepClone();
        }

        /// <summary>
        /// 设置此订属于该用户
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public virtual bool BelongTo(Customer customer)
        {
            this.Customer = customer;
            return true;
        }

        /// <summary>
        /// 支付网关调用
        /// </summary>
        /// <remarks>
        /// 支付网关需要独立存储txn信息
        /// </remarks>
        /// <param name="money"></param>
        public virtual void Paid(Money money)
        {
            if (!this.FlowNode.CanPay(this))
            {
                throw new InvalidOperationException();
            }
            if (money.Value < 0)
            {
                //因为RefundedAmount存储的正数，所以这里要减去money
                this.RefundedAmount = this.RefundedAmount - money;
            }
            else
            {
                this.CapturedAmount += money;
            }
            this.RecalculatePaymentStatus();
            this.FlowNode.FlowToNext(this);
        }

        /// <summary>
        /// 设置订单综合状态
        /// </summary>
        /// <param name="status"></param>
        public virtual void SetFlowNode(IOrderFlowNode status)
        {
            this.FlowNode = status;
        }
    }
}
